﻿using System;
using UnityEngine;

namespace RootMotion.FinalIK
{
	// Token: 0x0200001A RID: 26
	[HelpURL("https://www.youtube.com/watch?v=9MiZiaJorws&index=6&list=PLVxSIA1OaTOu8Nos3CalXbJ2DrKnntMv6")]
	[AddComponentMenu("Scripts/RootMotion.FinalIK/Grounder/Grounder Full Body Biped")]
	public class GrounderFBBIK : Grounder
	{
		// Token: 0x0600017F RID: 383 RVA: 0x0000316F File Offset: 0x0000136F
		[ContextMenu("TUTORIAL VIDEO")]
		public void OpenTutorial()
		{
			Application.OpenURL("https://www.youtube.com/watch?v=9MiZiaJorws&index=6&list=PLVxSIA1OaTOu8Nos3CalXbJ2DrKnntMv6");
		}

		// Token: 0x06000180 RID: 384 RVA: 0x00003109 File Offset: 0x00001309
		[ContextMenu("User Manual")]
		public override void OpenUserManual()
		{
			Application.OpenURL("http://www.root-motion.com/finalikdox/html/page11.html");
		}

		// Token: 0x06000181 RID: 385 RVA: 0x0000317B File Offset: 0x0000137B
		[ContextMenu("Scrpt Reference")]
		public override void OpenScriptReference()
		{
			Application.OpenURL("http://www.root-motion.com/finalikdox/html/class_root_motion_1_1_final_i_k_1_1_grounder_f_b_b_i_k.html");
		}

		// Token: 0x06000182 RID: 386 RVA: 0x00003187 File Offset: 0x00001387
		public override void Reset()
		{
			this.solver.Reset();
			this.spineOffset = Vector3.zero;
		}

		// Token: 0x06000183 RID: 387 RVA: 0x0000319F File Offset: 0x0000139F
		public bool IsReadyToInitiate()
		{
			return !(this.ik == null) && this.ik.solver.initiated;
		}

		// Token: 0x06000184 RID: 388 RVA: 0x0001FA1C File Offset: 0x0001DC1C
		public void Update()
		{
			this.firstSolve = true;
			this.weight = Mathf.Clamp(this.weight, 0f, 1f);
			if (this.weight <= 0f)
			{
				return;
			}
			if (this.initiated)
			{
				return;
			}
			if (!this.IsReadyToInitiate())
			{
				return;
			}
			this.Initiate();
		}

		// Token: 0x06000185 RID: 389 RVA: 0x000031CC File Offset: 0x000013CC
		public void FixedUpdate()
		{
			this.firstSolve = true;
		}

		// Token: 0x06000186 RID: 390 RVA: 0x000031CC File Offset: 0x000013CC
		public void LateUpdate()
		{
			this.firstSolve = true;
		}

		// Token: 0x06000187 RID: 391 RVA: 0x0001FA7C File Offset: 0x0001DC7C
		public void Initiate()
		{
			this.ik.solver.leftLegMapping.maintainRotationWeight = 1f;
			this.ik.solver.rightLegMapping.maintainRotationWeight = 1f;
			this.feet = new Transform[2];
			this.feet[0] = this.ik.solver.leftFootEffector.bone;
			this.feet[1] = this.ik.solver.rightFootEffector.bone;
			IKSolverFullBodyBiped solver = this.ik.solver;
			solver.OnPreUpdate = (IKSolver.UpdateDelegate)Delegate.Combine(solver.OnPreUpdate, new IKSolver.UpdateDelegate(this.OnSolverUpdate));
			this.solver.Initiate(this.ik.references.root, this.feet);
			this.initiated = true;
		}

		// Token: 0x06000188 RID: 392 RVA: 0x0001FB58 File Offset: 0x0001DD58
		public void OnSolverUpdate()
		{
			if (!this.firstSolve)
			{
				return;
			}
			this.firstSolve = false;
			if (!base.enabled)
			{
				return;
			}
			if (this.weight <= 0f)
			{
				return;
			}
			if (this.OnPreGrounder != null)
			{
				this.OnPreGrounder();
			}
			this.solver.Update();
			this.ik.references.pelvis.position += this.solver.pelvis.IKOffset * this.weight;
			this.SetLegIK(this.ik.solver.leftFootEffector, this.solver.legs[0]);
			this.SetLegIK(this.ik.solver.rightFootEffector, this.solver.legs[1]);
			if (this.spineBend != 0f)
			{
				this.spineSpeed = Mathf.Clamp(this.spineSpeed, 0f, this.spineSpeed);
				Vector3 a = base.GetSpineOffsetTarget() * this.weight;
				this.spineOffset = Vector3.Lerp(this.spineOffset, a * this.spineBend, Time.deltaTime * this.spineSpeed);
				Vector3 a2 = this.ik.references.root.up * this.spineOffset.magnitude;
				for (int i = 0; i < this.spine.Length; i++)
				{
					this.ik.solver.GetEffector(this.spine[i].effectorType).positionOffset += this.spineOffset * this.spine[i].horizontalWeight + a2 * this.spine[i].verticalWeight;
				}
			}
			if (this.OnPostGrounder != null)
			{
				this.OnPostGrounder();
			}
		}

		// Token: 0x06000189 RID: 393 RVA: 0x0001FD50 File Offset: 0x0001DF50
		public void SetLegIK(IKEffector effector, Grounding.Leg leg)
		{
			effector.positionOffset += (leg.IKPosition - effector.bone.position) * this.weight;
			effector.bone.rotation = Quaternion.Slerp(Quaternion.identity, leg.rotationOffset, this.weight) * effector.bone.rotation;
		}

		// Token: 0x0600018A RID: 394 RVA: 0x0001FDC0 File Offset: 0x0001DFC0
		public void OnDrawGizmosSelected()
		{
			if (this.ik == null)
			{
				this.ik = base.GetComponent<FullBodyBipedIK>();
			}
			if (this.ik == null)
			{
				this.ik = base.GetComponentInParent<FullBodyBipedIK>();
			}
			if (this.ik == null)
			{
				this.ik = base.GetComponentInChildren<FullBodyBipedIK>();
			}
		}

		// Token: 0x0600018B RID: 395 RVA: 0x0001FE24 File Offset: 0x0001E024
		public void OnDestroy()
		{
			if (this.initiated && this.ik != null)
			{
				IKSolverFullBodyBiped solver = this.ik.solver;
				solver.OnPreUpdate = (IKSolver.UpdateDelegate)Delegate.Remove(solver.OnPreUpdate, new IKSolver.UpdateDelegate(this.OnSolverUpdate));
			}
		}

		// Token: 0x040000B6 RID: 182
		[Tooltip("Reference to the FBBIK componet.")]
		public FullBodyBipedIK ik;

		// Token: 0x040000B7 RID: 183
		[Tooltip("The amount of spine bending towards upward slopes.")]
		public float spineBend = 2f;

		// Token: 0x040000B8 RID: 184
		[Tooltip("The interpolation speed of spine bending.")]
		public float spineSpeed = 3f;

		// Token: 0x040000B9 RID: 185
		[Tooltip("The spine bending effectors.")]
		public GrounderFBBIK.SpineEffector[] spine = new GrounderFBBIK.SpineEffector[0];

		// Token: 0x040000BA RID: 186
		public Transform[] feet = new Transform[2];

		// Token: 0x040000BB RID: 187
		public Vector3 spineOffset;

		// Token: 0x040000BC RID: 188
		public bool firstSolve;

		// Token: 0x0200001B RID: 27
		[Serializable]
		public class SpineEffector
		{
			// Token: 0x040000BD RID: 189
			[Tooltip("The type of the effector.")]
			public FullBodyBipedEffector effectorType;

			// Token: 0x040000BE RID: 190
			[Tooltip("The weight of horizontal bend offset towards the slope.")]
			public float horizontalWeight = 1f;

			// Token: 0x040000BF RID: 191
			[Tooltip("The vertical bend offset weight.")]
			public float verticalWeight;
		}
	}
}
